﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Sinacor.Infra.UI.Messaging.SmartClient.WeakEventManagers
{

    internal class OnErrorDeliveringExternalMessageWeakEventArgs : EventArgs
    {
        private Exception _ex;
        public Exception Ex
        {
            get { return _ex; }
            set { _ex = value; }
        }

        private External.ExternalMessage _ExternalMessage;
        public External.ExternalMessage ExternalMessage
        {
            get { return _ExternalMessage; }
            set { _ExternalMessage = value; }
        }

        public OnErrorDeliveringExternalMessageWeakEventArgs(Exception ex, External.ExternalMessage ExternalMessage)
        {
            _ex = ex;
            _ExternalMessage = ExternalMessage;
        }
    }

    internal class OnErrorDeliveringExternalMessageManager : WeakEventManager
    {
        private object _source;
        protected override void StartListening(object source)
        {
            MessageManager c = source as MessageManager;
            if (c != null)
            {
                c.OnErrorDeliveringExternalMessage += new MessageManager.ErrorDeliveringExternalMessage(OnErrorDeliveringExternalMessage);
                _source = source;
            }
        }

        protected override void StopListening(object source)
        {
            MessageManager c = source as MessageManager;
            if (c != null)
                c.OnErrorDeliveringExternalMessage -= new MessageManager.ErrorDeliveringExternalMessage(OnErrorDeliveringExternalMessage);

            _source = null;
        }

        void OnErrorDeliveringExternalMessage(Exception e, Sinacor.Infra.UI.Messaging.External.ExternalMessage ExternalMessage)
        {
            base.DeliverEvent(_source, new OnErrorDeliveringExternalMessageWeakEventArgs(e, ExternalMessage));
        }

        public static void AddListener(object source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(object source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedRemoveListener(source, listener);
        }

        private static OnErrorDeliveringExternalMessageManager CurrentManager
        {
            get
            {
                Type managerType = typeof(OnErrorDeliveringExternalMessageManager);
                OnErrorDeliveringExternalMessageManager currentManager = (OnErrorDeliveringExternalMessageManager)WeakEventManager.GetCurrentManager(managerType);
                if (currentManager == null)
                {
                    currentManager = new OnErrorDeliveringExternalMessageManager();
                    WeakEventManager.SetCurrentManager(managerType, currentManager);
                }
                return currentManager;
            }
        }
    }
}